<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button class="post-btn" :disabled="!message.length" @click="onPost"
      >发布</van-button
    >
  </div>
</template>

<script>
import { addComment } from "@/api/comment";
export default {
  name: "CommentPost",
  data() {
    return {
      message: ""
    };
  },
  props: {
    target: {
      type: [Number, String, Object],
      required: true
    },
    // 增加一个type，进行判断，如果是a则说明是文章回复，不需要使用inject注入的数据， 不是a则使用
    type: {
      type:String,
      validator (value){
        return ['a','c'].includes(value)
      },
      default:'a'
    }
  },
  // inject 接收父穿过来的
  inject: {
    articleId: {
      type: [Number, String, Object],
      default: null
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onPost() {
      try {
        const { data } = await addComment({
          target: this.target.toString(), //文章id
          content: this.message, //文章内容
         // art_id: null //文章id，对评论内容发表回复时，需要传递此参数，表明所属文章id。对文章进行评论，不要传此参数。
         art_id:this.type === 'a' ? null : this.articleId.toString()
        });
        console.log(data);
        this.message = "";
        this.$emit("post-success", data.data);
      } catch (err) {
        console.log(err);
        this.$toast.fail("发布失败");
      }
    }
  }
};
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
